package com.cet;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter;
import reactor.core.publisher.Mono;
import reactor.netty.http.server.HttpServer;

import java.io.IOException;

/**
 * 自定义服务器
 *
 */

@Slf4j
public class App 
{
    public static void main( String[] args ) throws IOException {
        HttpHandler httpHandler = (request, response) -> {

            log.info("{}: 接收到请求：{}", Thread.currentThread().getName(), request.getURI());

            DataBufferFactory dataBufferFactory = response.bufferFactory();
            DataBuffer dataBuffer = dataBufferFactory.wrap((request.getURI() + "========>Hello!").getBytes());

            return response.writeWith(Mono.just(dataBuffer));
        };

        // launch a server listen 8080
        ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(httpHandler);

        // netty server
        HttpServer.create().host("localhost").port(8080).handle(adapter).bindNow();

        log.info("服务器启动完成，监听8080端口，接收请求");

        System.in.read();

        log.info("服务器停止……");
    }
}
